package com.tongren.framework.maintabfragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

import com.tongren.framework.R;
import com.tongren.framework.core.BaseFragment;
import com.tongren.framework.core.ITabFragment;
import com.tongren.framework.sample.CoursewareListActivity;
import com.tongren.framework.sample.MyCoursewareListActivity;
import com.tongren.framework.sample.QAListActivity;
import com.tongren.framework.sample.TestListActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class SchoolFragment extends BaseFragment implements ITabFragment,View.OnClickListener{

    // 菜单
    private GridView gv_office;
    private List<Map<String, Object>> data_list;
    private SimpleAdapter sim_adapter;
    // 图片封装为一个数组
    private int[] icon = { R.drawable.ic_book2, R.drawable.ic_ask,
            R.drawable.ic_books, R.drawable.ic_yin};
    private String[] iconName = { "考试与作业", "咨询答疑", "课件", "我的课件", "试卷强化"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        enableLazyLoad();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main_school, container, false);
    }
    @Override
    public void setUpView(View view) {
        gv_office = (GridView)view.findViewById(R.id.gv_office);
    }

    @Override
    public void setUpData() {

        // grid数据
        //新建List
        data_list = new ArrayList<Map<String, Object>>();
        //获取数据
        getData();
        //新建适配器
        String [] from ={"image","text"};
        int [] to = {R.id.image,R.id.text};
        sim_adapter = new SimpleAdapter(this.getActivity(), data_list, R.layout.grid_item, from, to);
        //配置适配器
        gv_office.setAdapter(sim_adapter);
        gv_office.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                switch (position){
                    case 0:
                        Intent intent = new Intent(getActivity(), TestListActivity.class);
                        intent.putExtra("flag","t1");
                        startActivity(intent);
                        break;
                    case 1:
                        Intent intent2 = new Intent(getActivity(), QAListActivity.class);
                        intent2.putExtra("flag","t2");
                        startActivity(intent2);
                        break;
                    case 2:
                        Intent intent3 = new Intent(getActivity(), CoursewareListActivity.class);
                        intent3.putExtra("flag","t3");
                        startActivity(intent3);
                        break;
                    case 3:

                        Intent intent4 = new Intent(getActivity(), MyCoursewareListActivity.class);
                        intent4.putExtra("flag","t4");
                        startActivity(intent4);
                        break;
                    case 4:
//                        Intent intent5 = new Intent(getActivity(), NewListActivity.class);
//                        intent5.putExtra("flag","t5");
//                        startActivity(intent5);
                        break;
                    case 5:
//                        Intent intent6 = new Intent(getActivity(), NewListActivity.class);
//                        intent6.putExtra("flag","t6");
//                        startActivity(intent6);
                        break;
                    default:

//                        Toast.makeText(getActivity(),"1",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onMenuItemClick() {

    }

    @Override
    public BaseFragment getFragment() {
            return this;
    }

    public List<Map<String, Object>> getData(){
        //cion和iconName的长度是相同的，这里任选其一都可以
        for(int i=0;i<icon.length;i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", icon[i]);
            map.put("text", iconName[i]);
            data_list.add(map);
        }

        return data_list;
    }
}
